/*QByteArray j = json.readAll();
QTextCodec* defaultTextCodec = QTextCodec::codecForName("Windows-1251");
QTextCodec* htmlTextCodec = QTextCodec::codecForHtml(j, defaultTextCodec);
QString unicode = htmlTextCodec->toUnicode(j);
*/
//http://eu.battle.net/api/wow/item/18803?locale=ru_RU
//http://media.blizzard.com/wow/icons/18/ability_rogue_ambush.jpg
//http://eu.battle.net/api/wow/auction/data/eversong
#include "requestmanager.h"
RequestManager* RequestManager::p_instance = 0;
RequestManager::RequestManager(QObject *parent) :
    QObject(parent)
{

    domen = "eu";
    locale = "en_EN";   
    connect(this, &RequestManager::checkSendUrl,this, &RequestManager::getHordeDumpAuction );
    connect(this, &RequestManager::hordeDumpHere,[=]
            ()
    {
        qDebug()<<"hordeDumpHere signal emit";
    } );
}

RequestManager::~RequestManager()
{
    qDebug()<<"req deleted";
}

int RequestManager::getItemInfo( QString _itm)
{
    QNetworkAccessManager* _nam = new QNetworkAccessManager;
    QNetworkReply* reply;
    QString urlStr;
    urlStr.append("http://").append(domen).append(".battle.net/api/wow/item/").append( _itm ).append("?locale=").append(locale);
    url = urlStr;
    reply = _nam->get(QNetworkRequest(url));
    QObject::connect(_nam, &QNetworkAccessManager::finished,
                     [=] ()
    {
        if (reply->error() == QNetworkReply::NoError)
        {
            QJsonParseError  parseError;
            QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &parseError);

            if(parseError.error == QJsonParseError::NoError)
            {
                QMap<QString,QString> _map;
                _map.insert("id",jsonDoc.object().take("id").toVariant().toString());
                _map.insert("name",jsonDoc.object().take("name").toString());
                _map.insert("icon",jsonDoc.object().take("icon").toString());
                emit itemData(_map);                
            }
            else
            {
                qDebug()<<"-";
                emit myError("RequestManager.err- = "+parseError.errorString());
            }
        }
        else
        {
            qDebug()<<reply->errorString();
            QMap<QString,QString> _map;
            _map.insert("empty","empty");
            emit itemData(_map);
        }
    });

    return 1;
}

void RequestManager::checkUrl(const QString domen, const QString realm)
{
    QNetworkAccessManager* _nam = new QNetworkAccessManager;
    QNetworkReply* reply;
    QString urlStr;
    urlStr.append("http://").append(domen).append(".battle.net/api/wow/auction/data/").append( realm );
    qDebug()<<urlStr;
    url = urlStr;
    reply = _nam->get(QNetworkRequest(url));
    QObject::connect(_nam, &QNetworkAccessManager::finished,
                     [=] ()
    {
        if (reply->error() == QNetworkReply::NoError)
        {
            QJsonParseError  parseError;
            QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &parseError);

            if(parseError.error == QJsonParseError::NoError)
            {
                qDebug()<<"check url done";
                emit checkSendBool( (dumpLastModified < jsonDoc.object().take("files").toArray().at(0).toObject().take("lastModified").toVariant().toLongLong()));
                emit checkSendUrl( jsonDoc.object().take("files").toArray().at(0).toObject().take("url").toString() );
                dumpLastModified = jsonDoc.object().take("files").toArray().at(0).toObject().take("lastModified").toVariant().toLongLong();
            }
            else
            {
                emit myError("RequestManager.err = "+parseError.errorString());
            }
        }
        else
        {
            emit myError("RequestManager.err = "+reply->errorString());
        }
        delete reply;
    });
}

void RequestManager::getHordeDumpAuction( QUrl _u)
{
    qDebug()<<"getting dump as "<<_u;
  QNetworkAccessManager* _nam = new QNetworkAccessManager;
        QNetworkReply* reply;
    qDebug()<<".a";
    reply = _nam->get(QNetworkRequest(_u) );
    qDebug()<<".b";
    QObject::connect(_nam, &QNetworkAccessManager::finished,[=] ()
    {
        qDebug()<<".1";
        if (reply->error() == QNetworkReply::NoError)
        {
            qDebug()<<".2";
            QJsonParseError  parseError;
            QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &parseError);


 /*   QFile json("_data/json/json.txt");
    if(json.open(QIODevice::ReadOnly))
    {

        QJsonParseError  parseError;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(json.readAll(), &parseError);*/
        //----------
        if(parseError.error == QJsonParseError::NoError)
        {
            qDebug()<<".3";
            QJsonArray array;
            array = jsonDoc.object().take("horde").toObject().take("auctions").toArray();
            QList<QString> asT;
            for(auto iter = array.begin();iter!=array.end();  ++iter)
            {
                asT.append( (*iter).toObject().take("item"      ).toVariant().toString());
                asT.append( (*iter).toObject().take("bid"        ).toVariant().toString());
                asT.append( (*iter).toObject().take("buyout"  ).toVariant().toString());
                asT.append( (*iter).toObject().take("quantity").toVariant().toString());
                asT.append( (*iter).toObject().take("timeLeft").toVariant().toString());
                asT.append( (*iter).toObject().take("rand"     ).toVariant().toString());
                asT.append( (*iter).toObject().take("seed"     ).toVariant().toString());
            }
            qDebug()<<"dump is done";
            qDebug()<<asT.length();
            emit hordeDumpHere(asT);
            //------------
        }
        else
        {
            //  emit myError("RequestManager.err- = "+parseError.errorString());
            qDebug()<<"erorr";
        }
    }
    else
    {
        qDebug()<<"erorr";
        //    emit myError("RequestManager.err_ = "+reply->errorString());
    }
    //    delete reply;
});
qDebug()<<".c";
}
